package com.wfm.media;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;

public class Generator {

    @Test
    public void test(){
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/ruoyi-media1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true",
                        "root",
                        "wfm@1314")
                .globalConfig(builder -> builder
                        .author("wfm")
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/test/java")
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        .parent("data")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                        .entityBuilder()
                        .enableLombok()
                        .enableTableFieldAnnotation() // 启用字段注解
                        .enableFileOverride() // 覆盖已生成文件
                        .build()

                        .controllerBuilder()
                        .enableFileOverride() // 覆盖已生成文件
                        .build()

                        .serviceBuilder()
                        .enableFileOverride() // 覆盖已生成文件
                        .build()

                        .mapperBuilder()
                        .enableFileOverride() // 覆盖已生成文件
                        .build()

                        .controllerBuilder()
                        .enableFileOverride() // 覆盖已生成文件
                        .build()
                )

                .injectionConfig(injectConfig -> {
                    Map<String,Object> customMap = new HashMap<>();
                    customMap.put("abc","1234");
                    injectConfig.customMap(customMap); //注入自定义属性
                    injectConfig.customFile(new CustomFile.Builder()
                            .fileName("DTO.java") //文件名称
                            .templatePath("templates/entityDTO.java.ftl") //指定生成模板路径
                            .packageName("dto") //包名,自3.5.10开始,可通过在package里面获取自定义包全路径,低版本下无法获取,示例:package.entityDTO
                            .build());
                })

                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
